package com.kmust.dream.wanderingdream.donation.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.kmust.dream.wanderingdream.donation.entity.BasicInformationDonationEntity;
import com.kmust.dream.wanderingdream.donation.feign.NickName;
import com.kmust.dream.wanderingdream.donation.feign.UploadAdvice;
import com.kmust.dream.wanderingdream.donation.service.BasicInformationDonationService;
import com.kmust.dream.wanderingdream.donation.utils.JwtUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kmust.dream.wanderingdream.donation.entity.DonationThingsEntity;
import com.kmust.dream.wanderingdream.donation.service.DonationThingsService;
import com.kmust.dream.wanderingdream.comm.utils.PageUtils;
import com.kmust.dream.wanderingdream.comm.utils.R;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author Leng
 * @email Leng81605@gmail.com
 * @date 2021-07-12 21:35:52
 */
@RestController
@Api(tags = "捐赠")
@RequestMapping("donation/donationthings")
public class DonationThingsController {
    @Autowired
    private DonationThingsService donationThingsService;
    @Autowired
    UploadAdvice uploadAdvice;
    @Autowired
    BasicInformationDonationService basicInformationDonationService;
    @Autowired
    NickName nickName;
    @Autowired

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = donationThingsService.queryPage(params);
        List<DonationThingsEntity> list = donationThingsService.getList();
        for (int i=0;i<list.size();i++){
            if (list.get(i).getNickName()==null){
                String nickname = nickName.getNickname(list.get(i).getDonationName());
                list.get(i).setNickName(nickname);
            }
        }
        page.setList(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{donationId}")

    public R info(@PathVariable("donationId") Integer donationId){
		DonationThingsEntity donationThings = donationThingsService.getById(donationId);

        return R.ok().put("donationThings", donationThings);
    }

    /**
     * 保存
     */
    @PostMapping("/save")

    public R save(DonationThingsEntity donationThings, HttpServletRequest servletRequest, @RequestParam("file") MultipartFile multipartFile){
        if (multipartFile.isEmpty()){
            donationThings.setDonationImg(null);
        }else {
            String url = uploadAdvice.getUrl(multipartFile);
            donationThings.setDonationImg(url);
        }
        String username = JwtUtils.getUserUNByJwtToken(servletRequest);
        String nickname = nickName.getNickname(username);
        donationThings.setDonationName(username);
        donationThings.setNickName(nickname);
        donationThingsService.save(donationThings);
        return R.ok();
    }
    @PostMapping("/donationMoney")
    public R saveRe(DonationThingsEntity donationThings,HttpServletRequest servletRequest){
           if (donationThings.getDonationMoney()>0){
               BasicInformationDonationEntity base = basicInformationDonationService.getById(donationThings.getRecipientId());
               if (base.getTargetMoney()>=base.getTotalMoney()+donationThings.getDonationMoney()){
                   base.setTotalMoney(base.getTotalMoney()+donationThings.getDonationMoney());
                   basicInformationDonationService.updateById(base);
               }
               else
                   return R.ok("此捐赠已经完成,感谢您的爱心!");

           }
           String username = JwtUtils.getUserUNByJwtToken(servletRequest);
           System.out.println("++++++++*******"+servletRequest.getHeader("token"));
           String nickname = nickName.getNickname(username);
           donationThings.setDonationName(username);
           donationThings.setNickName(nickname);
           System.out.println(donationThings);
           donationThingsService.save(donationThings);
           return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")

    public R update(@RequestBody DonationThingsEntity donationThings){
		donationThingsService.updateById(donationThings);

        return R.ok();
    }
    @GetMapping("/getByNickName/{nickname}")
    public List<DonationThingsEntity> getByNickName(@PathVariable("nickname") String nickname){
        return donationThingsService.getListByNickName(nickname);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")

    public R delete(@RequestBody Integer[] donationIds){
		donationThingsService.removeByIds(Arrays.asList(donationIds));

        return R.ok();
    }
    @GetMapping("/getByUserName/{username}")
    public List<DonationThingsEntity> getByName(@PathVariable("username") String username){
        return donationThingsService.getListByUserName(username);

    }

}
